# coding=utf-8
#
# 版权所有 2011 沙发客团队
# 遵循Apache License 2.0协议
#    具体见 http://www.apache.org/licenses/LICENSE-2.0
#
import wx, sys

from logger import Logger
from const import *


class NotificationEvent(wx.PyEvent):
    """Simple event to carry arbitrary result data."""
    def __init__(self, data, key = None, key2 = None):
        """Init Result Event."""
        wx.PyEvent.__init__(self)
        self.SetEventType(EVT_NOTIFICATION_ID)
        self.data       = data
        self.key        = key
        self.key2       = key2

class NotificationManager:
    # =============================================
    # 唯一实例开始位置
    # =============================================
    class __impl:
        """ Implementation of the singleton interface """

    # storage for the instance reference
    __instance = None
    
    def __getattr__(self, attr):
        """ Delegate access to implementation """
        return getattr(self.__instance, attr)

    def __setattr__(self, attr, value):
        """ Delegate access to implementation """
        return setattr(self.__instance, attr, value)
    # =============================================
    # 唯一实例结束位置
    # =============================================
    
    def __init__(self):
        """ Create singleton instance """
        if NotificationManager.__instance is None:
            NotificationManager.__instance = NotificationManager.__impl()
            self._logger           = Logger()
            
            self._frame            = None
            self._logger.info(u"开始初始化 通知事件管理器")

            #
            # 记录上一次的通知事件
            #
            self._last_action      = NOTIFICATION_NONE

    def SetWindowsFram(self, frame):
        """设置消息对话框"""
        self._frame = frame
    
    def UpdateShafaMessage(self, value):
        """更新信息"""
        if self._frame is None:
            return
        
        if self._last_action == NOTIFICATION_UPDATE_MESS:
            return
        wx.PostEvent(self._frame, NotificationEvent(NOTIFICATION_UPDATE_MESS, value))
        self._last_action  = NOTIFICATION_NONE
     
    def UpdateShafaCount(self, rt_count, comment_count):
        """更新信息"""
        if self._frame is None:
            return
        
        if self._last_action == NOTIFICATION_UPDATE_LABLE:
            return
        wx.PostEvent(self._frame, NotificationEvent(NOTIFICATION_UPDATE_LABLE, rt_count, comment_count))
        self._last_action  = NOTIFICATION_NONE
        
    def UpdateShafaSleepTime(self, value, sleep_time):
        """ 通知UI休眠时间 """
        if self._frame is None:
            return
        
        if self._last_action == NOTIFICATION_UPDATE_TIME:
            return
        
        wx.PostEvent(self._frame, NotificationEvent(NOTIFICATION_UPDATE_TIME, value, sleep_time))
        self._last_action  = NOTIFICATION_NONE
         
    def UpdateRushTime(self, text, rush_time):
        """ 通知开始抢沙发的有效时间 """
        
        if self._frame is None:
            return
        
        if self._last_action == NOTIFICATION_RUSH_TIME:
            return
        
        wx.PostEvent(self._frame, 
            NotificationEvent(NOTIFICATION_RUSH_TIME, text, rush_time))
        self._last_action  = NOTIFICATION_NONE
    